package org.dromara.basic.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.basic.domain.bo.TeachClassBo;
import org.dromara.basic.domain.vo.MemberVo;
import org.dromara.basic.domain.vo.TeachClassVo;
import org.dromara.basic.listener.MemberInfoListener;
import org.dromara.basic.listener.TeachClassListener;
import org.dromara.basic.service.ITeachClassService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 教学班级
 *
 * @author chl
 * @date 2024-09-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/basic/teachClass")
public class TeachClassController extends BaseController {

    private final ITeachClassService teachClassService;

    /**
     * 查询教学班级列表
     */
    @SaCheckPermission("basic:teachClass:list")
    @GetMapping("/list")
    public TableDataInfo<TeachClassVo> list(TeachClassBo bo, PageQuery pageQuery) {
        return teachClassService.queryPageList(bo, pageQuery);
    }

    /**
     * 根据专业查询班级列表
     */
    @SaIgnore
    @GetMapping("/list_by_major")
    public R<List<TeachClassVo>> listByMajor(Integer majorId) {
        return  R.ok(teachClassService.queryListByMajor(majorId));
    }


    //SELECT DISTINCT class_id, class_name FROM phy_basic_teach_class t ,sys_dept d WHERE t.dept_id = d.dept_id AND d.parent_id = 112

    /**
     * 列表查询-教学班级列表
     */
    @SaCheckPermission("basic:teachClass:list")
    @GetMapping("/listInfo")
    public R<List<TeachClassVo>> list(TeachClassBo bo) {
        return R.ok(teachClassService.queryList(bo)) ;
    }

    /**
     * 导出教学班级列表
     */
    @SaCheckPermission("basic:teachClass:export")
    @Log(title = "教学班级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TeachClassBo bo, HttpServletResponse response) {
        List<TeachClassVo> list = teachClassService.queryList(bo);
        AtomicInteger index = new AtomicInteger(1);
        list.forEach(vo-> {
            // 序号递增
            vo.setIndex(index.getAndIncrement());
        });
        ExcelUtil.exportExcel(list, "教学班级", TeachClassVo.class, response);
    }

    /**
     * 获取教学班级详细信息
     *
     * @param classId 主键
     */
    @SaCheckPermission("basic:teachClass:query")
    @GetMapping("/{classId}")
    public R<TeachClassVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long classId) {
        return R.ok(teachClassService.queryById(classId));
    }


    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "导入班级信息", businessType = BusinessType.IMPORT)
//    @SaCheckPermission("basic:teachClass:import")
    @SaIgnore
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<TeachClassVo> result = ExcelUtil.importExcel(file.getInputStream(), TeachClassVo.class, new TeachClassListener(updateSupport));
        return R.ok(result.getAnalysis());
    }
    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    @SaIgnore
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "班级信息导入模板", TeachClassVo.class, response);
    }

    /**
     * 新增教学班级
     */
    @SaCheckPermission("basic:teachClass:add")
    @Log(title = "教学班级", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TeachClassBo bo) {
        return toAjax(teachClassService.insertByBo(bo));
    }

    /**
     * 修改教学班级
     */
    @SaCheckPermission("basic:teachClass:edit")
    @Log(title = "教学班级", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TeachClassBo bo) {
        return toAjax(teachClassService.updateByBo(bo));
    }

    /**
     * 删除教学班级
     *
     * @param classIds 主键串
     */
    @SaCheckPermission("basic:teachClass:remove")
    @Log(title = "教学班级", businessType = BusinessType.DELETE)
    @DeleteMapping("/{classIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] classIds) {
        return toAjax(teachClassService.deleteWithValidByIds(List.of(classIds), true));
    }
}
